package com.kws.merchant.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.security.auth.AuthUtil;
import com.kws.merchant.domain.LoanApplicationEntity;
import com.kws.merchant.mapper.LoanApplicationMapper;
import com.kws.merchant.service.LoanApplicationService;
import com.kws.merchant.util.FileUtil;
import com.kws.system.api.domain.SysUser;
import org.springframework.stereotype.Service;

/**
 * @author : zhangbenting
 * @date : 2024/12/25 16:52
 * @Version: 1.0
 */
@Service
public class LoanApplicationServiceImpl extends ServiceImpl<LoanApplicationMapper, LoanApplicationEntity>
        implements LoanApplicationService {

    @Override
    public Boolean loanApplication(LoanApplicationEntity loanApplicationEntity) {
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        Long userId = sysUser.getUserId();
        loanApplicationEntity.setUserId(userId);
        if (loanApplicationEntity.getUserType() == 1){
            if (StringUtils.isEmpty(loanApplicationEntity.getCertificateNumber())) {
                throw new RuntimeException("请上传证件号码");
            }
            if (StringUtils.isEmpty(loanApplicationEntity.getCertificateFront())) {
                throw new RuntimeException("请上传证件正面");
            }
            if (StringUtils.isEmpty(loanApplicationEntity.getCertificateBack())) {
                throw new RuntimeException("请上传证件背面");
            }
            loanApplicationEntity.setCertificateFront(FileUtil.subFileName(loanApplicationEntity.getCertificateFront()));
            loanApplicationEntity.setCertificateBack(FileUtil.subFileName(loanApplicationEntity.getCertificateBack()));
            return this.save(loanApplicationEntity);
        } else if (loanApplicationEntity.getUserType() == 0) {
            if (StringUtils.isEmpty(loanApplicationEntity.getLoanEnterprise())) {
                throw new RuntimeException("请上传企业名称");
            }
            if (StringUtils.isEmpty(loanApplicationEntity.getSocietyNumber())) {
                throw new RuntimeException("请上传社会编号");
            }
            if (StringUtils.isEmpty(loanApplicationEntity.getBusinessLicense())) {
                throw new RuntimeException("请上传营业执照");
            }
            loanApplicationEntity.setBusinessLicense(FileUtil.subFileName(loanApplicationEntity.getBusinessLicense()));
            return this.save(loanApplicationEntity);
        }
        return false;
    }

}
